package redis2

import (
	"context"
	"gitee.com/chejiangyi/bsfgo/core/base2"
	"github.com/redis/go-redis/v9"
)

var RedisUtil = &RedisUtilTool{}

type RedisUtilTool struct {
}

// NewRedisClient 创建新的 Redis 客户端
func (m *RedisUtilTool) NewRedisClient() *RedisClient {
	option := RedisOptions{
		Addr:        base2.BsfConfigInstance.GetRedisAddr(),
		Db:          base2.BsfConfigInstance.GetRedisDb(),
		Username:    base2.BsfConfigInstance.GetRedisUsername(),
		Password:    base2.BsfConfigInstance.GetRedisPassword(),
		PoolSize:    base2.BsfConfigInstance.GetRedisMaxPoolSize(),
		MinPoolSize: base2.BsfConfigInstance.GetRedisMinPoolSize(),
		MaxPoolSize: base2.BsfConfigInstance.GetRedisMaxPoolSize(),
	}
	return m.NewRedisClient2(option)
}
func (*RedisUtilTool) NewRedisClient2(option RedisOptions) *RedisClient {
	rc := redis.NewClient(&redis.Options{
		Addr:         option.Addr,
		DB:           option.Db,
		Username:     option.Username,
		Password:     option.Password,
		PoolSize:     option.MaxPoolSize,
		MinIdleConns: option.MinPoolSize,
		MaxIdleConns: option.MaxPoolSize,
	})

	// 测试连接是否正常
	ctx := context.Background()
	if _, err := rc.Ping(ctx).Result(); err != nil {
		panic(base2.NewBsfError2("redis2 连接异常", err))
	}

	return &RedisClient{
		Client:  rc,
		context: ctx,
	}
}
